<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Miniappsorder;
use App\Models\Keyorder;
use App\Models\Users;
use App\Services\OutputService;
use Auth,Pay,Log,Exception;

class OrderController extends Controller
{
    public function pay(Request $request)
    {
        return view("admin.order.pay");
    }

    public function pcPay(Request $request)
    {
        $token = $request->token;
        try {
            $users_id = decrypt($token);
            Auth::loginUsingId($users_id);
        } catch (Exception $e) {
            dd("报错");
        }
        if ($request->corp_type == "qy") {
            $corp_type = 1;
        } elseif($request->corp_type == "gt") {
            $corp_type = 2;
        } else {
            $corp_type = 3;
        }
        $corp_name = $request->corp_name;
        $phone = $request->phone;
        return view("admin.order.pc_pay",["corp_type"=>$corp_type,"corp_name"=>$corp_name,"phone"=>$phone]);
    }

    public function list(Request $request)
    {
        $users = Auth::user();
        $miniappOrders = Miniappsorder::userMiniappOrderList($users->id);
        return view("admin.order.order_list",["miniappOrders"=>$miniappOrders]);
    }

    public function key(Request $request)
    {
        return view("admin.order.key");
    }

    public function keyActive(Request $request)
    {
        if (empty($request->key) || mb_strlen($request->key) != 32) return OutputService::error("激活码错误,请输入正确的激活码!");
        $keyList = Keylist::keyListDetial($request->key);
        if (empty($keyList)) return OutputService::error("激活码错误,请输入正确的激活码!");
        if ($keyList->status == 2) return OutputService::error("激活码已使用,无法重复激活!");
        $users = Auth::user();
        Keylist::keyListActive($request->key,$users->id,2);
        Users::incrementUserScore($users->id,$keyList->score);
        return OutputService::success("激活码激活成功!");
    }

    public function paySubmit(Request $request)
    {
        $users  = Auth::user();
        $users_id = $users->id;
        $order_id = md5(time().rand("10000","99999"));
        if ($request->corp_type == 1) {
            $corpPrice = 0;
            $title = "企业";
        }elseif($request->corp_type == 2) {
            $corpPrice = 0;
            $title = "个体";
        } else {
            $corpPrice = 0;
            $title = "个人";
        }
        if ($request->service_type == 1) {
            $servicePrice = 100;
            $title .= "-专属客服版";
        }elseif($request->service_type == 2) {
            $servicePrice = 100;
            $title .= "-代备案版";
        } else {
            $servicePrice = 100;
            $title .= "-自助版";
        }

        $price = round(($corpPrice+$servicePrice),2);
        $title = "备案宝-".$title;
        $lastOrder = Miniappsorder::lastMiniappOrder($users_id,$request->corp_type,$request->service_type);
        if (!empty($lastOrder)) return OutputService::error("当前存在本类型已支付的订单,请先完成小程序备案后再下单新订单!");
        Miniappsorder::addMiniappOrder($users_id,$order_id,$title,$request->corp_type,$request->service_type,$price);
        if ($request->payment_id == 2) {
            $order = [
                'out_trade_no' => $order_id,
                'total_amount' => $price,
                'subject' => $title,
            ];
            $payConfig = config("pay.alipay");
            // if ($request->device == "phone") {
            //     return Pay::alipay($payConfig)->wap($order);
            // } else {
                return Pay::alipay($payConfig)->web($order);
            // }
        } else {
            $order = [
                'out_trade_no' => $order_id,
                'body' => $title,
                'total_fee' => $price*100,
            ];
            $config = config('pay.wechat');
            if ($request->wechat == 1) {
                $order['openid'] = $users->wechat_openid;
                try {
                    $result = Pay::wechat($config)->mp($order);
                    return response()->json(["status"=>"success","code"=>"1000","msg"=>"生成成功！","data"=>$result]);
                    return OutputService::error("发起支付失败,请使用电脑支付!");
                } catch (Exception $e) {
                    return response()->json(["status"=>"error","code"=>"1002","msg"=>"发起支付失败,请使用电脑支付!","data"=>[]]);
                    return OutputService::error("发起支付失败,请使用电脑支付!");
                }
            } else {
                $result = Pay::wechat($config)->scan($order);
            }
            return OutputService::success("SUCCESS",["out_trade_no"=>$order_id,"code_url"=>$result->code_url,"price"=>$price]);
        }
    }

    public function payStatus(Request $request)
    {
        $users  = Auth::user();
        $users_id = $users->id;      
        $miniappOrder = Miniappsorder::miniappOrderPayStatus($users_id,$request->order_id);
        if (empty($miniappOrder)) return OutputService::error("订单不存在");
        if ($miniappOrder->pay_status == 1) return OutputService::error("订单未支付");
        return OutputService::success("SUCCESS");
    }
}
